﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using ECMCommMan;
using CSVFileHandler;

namespace ECMSim
{
    public partial class FormMain
    {
        #region find the row number for a given string in Name field
        /// <summary>
        /// <param> </param>
        /// <returns> </returns>
        private int findRowNumber(string sRunTimeDataName)
        {
            DataGridViewRow nRowNumber = (from DataGridViewRow row in dgRunTimeData.Rows
                                          where row.Cells["Name"].Value.ToString().Equals(sRunTimeDataName)
                                          select row).First();
            return nRowNumber.Index;
        }
        #endregion

        #region Timer event for all Gauges
        /// <summary>
        /// <param> </param>
        /// <returns> </returns>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(rbECM.Checked == true)
            {
                int nRowNumber;

                nRowNumber = findRowNumber("Air Temperature");
                aGaugeChangeValue(ref aGaugeInletAirTemp, 5, nRowNumber);

                nRowNumber = findRowNumber("Engine Temperature");
                aGaugeChangeValue(ref aGaugeEngineTemp, 5, nRowNumber);

                nRowNumber = findRowNumber("Battery Voltage");
                aGaugeChangeValue(ref aGaugeVoltage, 1, nRowNumber);
            }
            if(rbDash.Checked == true)
            {
                if (btnRequestLiveData.Text == "Stop")
                {
                    ecmCommMan.ECMDataRequest((byte)ECMCommunicationManager.ECMCommandType.ECMRunTimeData);
                }
            }
        }
        #endregion

        #region Change the value for a given Gauge
        /// <summary>
        /// <param> </param>
        /// <returns> </returns>
        private void aGaugeChangeValue(ref AGaugeApp.AGauge aGaugeRef, int nValueChange, int nRowNumber)
        {
            string sTemp = "";
            float fTemp = 0;
            int nGaugeValueRaw = 0;
            float fScalingFactor = 0;
            int nTranslation = 0;

            if (aGaugeRef.NeedleDirection == "UP")
            {
                aGaugeRef.Value += nValueChange;
            }
            else
            {
                aGaugeRef.Value -= nValueChange;
            }

            if (aGaugeRef.Value >= aGaugeRef.RangesStartValue[2] && aGaugeRef.Value <= aGaugeRef.RangesEndValue[2])
            {
                aGaugeRef.NeedleColor1 = AGaugeApp.AGauge.NeedleColorEnum.Blue;
                aGaugeRef.NeedleColor2 = Color.DeepSkyBlue;
            }
            if (aGaugeRef.Value >= aGaugeRef.RangesStartValue[1])
            {
                aGaugeRef.NeedleColor1 = AGaugeApp.AGauge.NeedleColorEnum.Red;
                aGaugeRef.NeedleColor2 = Color.Red;
            }
            if (aGaugeRef.Value >= aGaugeRef.RangesStartValue[0] && aGaugeRef.Value <= aGaugeRef.RangesEndValue[0])
            {
                aGaugeRef.NeedleColor1 = AGaugeApp.AGauge.NeedleColorEnum.Green;
                aGaugeRef.NeedleColor2 = Color.Lime;
            }

            sTemp = (string)dgRunTimeData["Scaling Factor", nRowNumber].Value;
            fScalingFactor = Convert.ToSingle(sTemp);
            sTemp = (string)dgRunTimeData["Translation", nRowNumber].Value;
            nTranslation = Convert.ToInt16(sTemp);

            fTemp = ((aGaugeRef.Value - nTranslation) / fScalingFactor);
            nGaugeValueRaw = Convert.ToInt16(fTemp);

            if (aGaugeRef.Name == "aGaugeInletAirTemp")
            {
                tbInletAirTempHex.Text = "0x" + nGaugeValueRaw.ToString("X");
                tbInletAirTempValue.Text = aGaugeRef.Value.ToString() + " degC";
            }
            if (aGaugeRef.Name == "aGaugeEngineTemp")
            {
                tbEngineTempHex.Text = "0x" + nGaugeValueRaw.ToString("X");
                tbEngineTempValue.Text = aGaugeRef.Value.ToString() + " degC";
            }
            if (aGaugeRef.Name == "aGaugeVoltage")
            {
                tbVoltageHex.Text = "0x" + nGaugeValueRaw.ToString("X");
                tbVoltageValue.Text = aGaugeRef.Value.ToString() + " V";
            }
            WriteHexValueToTable("Hex_Data", nRowNumber, nGaugeValueRaw);

            //gbEngineTemp.get
        }
        #endregion

        #region Value in Range of a given Gauge changed
        /// <summary>
        /// <param> </param>
        /// <returns> </returns>
        private void aGaugeInletAirTemp_ValueInRangeChanged(object sender, AGaugeApp.AGauge.ValueInRangeChangedEventArgs e)
        {
            if (aGaugeInletAirTemp.Value == aGaugeInletAirTemp.MaxValue)
            {
                aGaugeInletAirTemp.NeedleDirection = "DOWN";
            }
            if (aGaugeInletAirTemp.Value == aGaugeInletAirTemp.MinValue)
            {
                aGaugeInletAirTemp.NeedleDirection = "UP";
            }
        }

        private void aGaugeEngineTemp_ValueInRangeChanged(object sender, AGaugeApp.AGauge.ValueInRangeChangedEventArgs e)
        {
            if (aGaugeEngineTemp.Value == aGaugeEngineTemp.MaxValue)
            {
                aGaugeEngineTemp.NeedleDirection = "DOWN";
            }
            if (aGaugeEngineTemp.Value == aGaugeEngineTemp.MinValue)
            {
                aGaugeEngineTemp.NeedleDirection = "UP";
            }
        }

        private void aGaugeVoltage_ValueInRangeChanged(object sender, AGaugeApp.AGauge.ValueInRangeChangedEventArgs e)
        {
            if (aGaugeVoltage.Value == aGaugeVoltage.MaxValue)
            {
                aGaugeVoltage.NeedleDirection = "DOWN";
            }
            if (aGaugeVoltage.Value == aGaugeVoltage.MinValue)
            {
                aGaugeVoltage.NeedleDirection = "UP";
            }
        }
        #endregion
    }
}